package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderListService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersettinglist")
public class OrderSettingListController {

    @Reference
    private OrderListService orderListService;

    //1.页面加载，分页(条件)查询;
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult=null;

        try {
            PageInfo<Map<String, Object>> pageInfo = orderListService.findByCondition(queryPageBean);
            long total = pageInfo.getTotal();
            List<Map<String, Object>> list = pageInfo.getList();
            pageResult= new PageResult(total, list);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResult;
    }

    //2.1新增预约(用户手机预约);
    @RequestMapping("/findAll")
    public Result findAll() {
        List<Setmeal> list;
        try {
            list = orderListService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, null);
        }
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
    }

    //2.2新增预约(用户手机预约);
    @RequestMapping("/add")
    public Result add( @RequestBody Map map ,int[] setmealIds ) {
        Result result;
        try {
            map.put("orderType", Order.ORDERTYPE_TELEPHONE);
            //设置预约类型，分为微信预约、电话预约
            //1.用户预约
            result = orderListService.order(map,setmealIds);
            //2.用户预约成功后，给出预约成功信息(体检人姓名、预约日期、套餐名称、预约类型)
          orderListService.findById((Integer) result.getData());
            //3.后台测试预约成功短信通知；
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增预约失败！");
        }
        return result;
    }


    //3.确定预约(用户确认预约，到诊状态：未到诊-->已到诊)；
    @RequestMapping("/updateById")
    public Result fndById(Integer id) {

        try {
            orderListService.updateById(id);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"确认预约失败!");
        }
        return new Result(true, "您已确认预约成功！");
    }


    //4.取消预约(删除用户已预约数据);
    @RequestMapping("/deleteById")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result deleteById(Integer id) {
        Result result;
        try {
            //取消预约成功
           result = orderListService.deleteById(id);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"取消预约失败！");
        }
        return result;
    }

    //5.批量取消预约(复选框预约数据批量删除)；
    //获取前段页面发送的请求参数 int[] ids,循环遍历，调用取消预约的方法
    @RequestMapping("/deleteByIds")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result deleteByIds(Integer id) {
       return orderListService.deleteById(id);
    }
}
